<template>
    <div>
        <el-dialog
            title="开收据"
            :visible.sync="dialogVisible"
            width="1200px"
             :close-on-click-modal="false"
        >
            <el-row style="text-align: right"><el-button type="primary" size="small" @click="confirm">下载</el-button></el-row>
            <div class="center" id="printReceiptTemplate">

                <el-row class="">
                    <span style="font-size: 18px">单据痕迹：</span>
                    <span v-if="isY">原单</span>
                    <span v-else>伪原单</span>
                </el-row>
                <el-row style="text-align: center;padding-bottom: 20px">
                    <span style="font-size: 30px">{{form.contractName}}</span>
                </el-row>
                <el-row class="row">
                    <el-col :span="12">
                        <span>客户：</span><el-input readonly v-model="form.costProjectName" style="width: 40%"></el-input>
                    </el-col>
                    <el-col :span="12" style="text-align: right;">
                        <span>NO:</span><el-input readonly v-model="number" style="width: 60%"></el-input>
                    </el-col>
                </el-row>
                <el-table :data="tableData"
                          show-summary
                          :summary-method="getSummaries"
                          class="table">
                    <el-table-column label="序号" type="index"  width="80"></el-table-column>
                    <el-table-column label="收费项目" prop="chargeProjectName"></el-table-column>
                    <el-table-column label="费用期间" prop="costDate">
                    </el-table-column>
                    <el-table-column label="应收金额" prop="receivableMoney"></el-table-column>
                    <el-table-column label="实收金额" prop="collectionAmount"></el-table-column>
                    <el-table-column label="合计" prop="collectionAmount">
                    </el-table-column>
                </el-table>
                <el-row class="row">
                    <el-col :span="6">
                        <span>收款人：</span><el-input readonly style="width: 60%"></el-input>
                    </el-col>
                    <el-col :span="6">
                        <span>交款日期：</span><el-input readonly v-model="form.collectionDate" style="width: 60%"></el-input>
                    </el-col>
                    <el-col :span="6">
                        <span>交款人（签字）：</span><el-input readonly  style="width: 40%"></el-input>
                    </el-col>
                    <el-col :span="6">
                        <span>打印日期：</span><el-input readonly  style="width: 60%"></el-input>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="16">
                        <span>代收单位盖章：</span><label></label>
                    </el-col>
                    <el-col :span="4">
                        <span>打印人：</span><label>{{user.realName}}</label>
                    </el-col>
                    <el-col :span="4">
                        <span>打印次数：</span><label>{{sum}}</label>
                    </el-col>
                </el-row>
            </div>
            <div style="text-align: right;padding-top: 40px">
                <el-button type="primary" size="small" @click="rest">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {addObj,getBillFlow,getFinanceBillFlowByBillNumber} from '../../../api/finance-bill-flow'
    export default {
        data(){
            return{
                dialogVisible:false,
                isY:true,
                form:{},
                tableData:[
                    {
                        collectionAmount:'',
                        receivableMoney:'',
                        costDate:'',
                        chargeProjectName:''
                    }
                ],
                number:'',
                user:'',
                sum:0
            }
        },

        methods:{
            getSummaries(param) {
                const sums =[]
                sums[0] = '合计:'
                sums[1] =this.toChies(this.form.collectionAmount)
                return sums;
                // sums数组的每一个元素代表表格从左到右的列(column)。
            },
            getDate(){
                var date=new Date()
                return date.getFullYear()+((date.getMonth()+1)>=10?(date.getMonth()+1):'0'+(date.getMonth()+1))+(date.getDate()<10?'0'+date.getDate():date.getDate())
            },
            show(item){
               if(item){
                   this.form=item
                   this.tableData[0].collectionAmount=item.collectionAmount
                   this.tableData[0].receivableMoney=item.receivableMoney
                   this.tableData[0].costDate=item.costPeriodStart+'至'+item.costPeriodEnd
                   this.tableData[0].chargeProjectName=item.chargeProjectName
                   let billNumber='SJ'+this.getDate()
                   this.dialogVisible=true
                   this.user=JSON.parse(JSON.parse(localStorage.getItem('dbl-user')).content)
                   getBillFlow(item.actualId).then(res=>{
                       let data=res.data.data
                       if(data.length>0){
                           this.isY=false
                           this.sum=data.length
                       }
                   })
                   getFinanceBillFlowByBillNumber({billNumber:billNumber}).then(res=>{
                       this.number=res.data.msg
                   })
               }
            },
            rest(){
                this.dialogVisible=false
                this.isY=true
                this.form={}
                this.tableData=[
                    {
                        collectionAmount:'',
                        receivableMoney:'',
                        costDate:'',
                        chargeProjectName:''
                    }
                ]
                    this.number=''
                    this.user=''
                    this.sum=0
            },
            confirm(){
                console.log(this.form)
                addObj(Object.assign(this.form,{billNumber:this.number})).then(res=>{
                    this.suc('添加成功')
                    this.dowload()
                    this.rest()
                })
            },
            getDateTime(){
                var date=new Date()
                return date.getFullYear()+((date.getMonth()+1)>=10?(date.getMonth()+1):'0'+(date.getMonth()+1))+(date.getDate()<10?'0'+date.getDate():date.getDate())
                    +(date.getHours()<10?'0'+date.getHours():date.getDate())+(date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes())+(date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds())
            },
            dowload(){
                this.ExportSavePdf('printReceiptTemplate',this.getDateTime())
            },
            toChies: function(amount) { //形参
                // 汉字的数字
                const cnNums = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
                // 基本单位
                const cnIntRadice = ["", "拾", "佰", "仟"];
                // 对应整数部分扩展单位
                const cnIntUnits = ["", "万", "亿", "兆"];
                // 对应小数部分单位
                const cnDecUnits = ["角", "分"];
                // 整数金额时后面跟的字符
                const cnInteger = "整";
                // 整型完以后的单位
                const cnIntLast = "元";
                // 最大处理的数字
                const maxNum = 9999999999999999.99;
                // 金额整数部分
                let integerNum;
                // 金额小数部分
                let decimalNum;
                // 输出的中文金额字符串
                let chineseStr = "";
                // 分离金额后用的数组，预定义
                let parts;
                if (amount === "") {
                    return "";
                }
                amount = parseFloat(amount);
                if (amount >= maxNum) {
                    // 超出最大处理数字
                    return "";
                }
                if (amount === 0) {
                    chineseStr = cnNums[0] + cnIntLast + cnInteger;
                    return chineseStr;
                }
                // 转换为字符串
                amount = amount.toString();
                if (amount.indexOf(".") === -1) {
                    integerNum = amount;

                    decimalNum = "";
                } else {
                    parts = amount.split(".");
                    integerNum = parts[0];
                    decimalNum = parts[1].substr(0, 4);
                }
                // 获取整型部分转换
                if (parseInt(integerNum, 10) > 0) {
                    let zeroCount = 0;
                    const IntLen = integerNum.length;
                    for (let i = 0; i < IntLen; i++) {
                        const n = integerNum.substr(i, 1);
                        const p = IntLen - i - 1;
                        const q = p / 4;
                        const m = p % 4;
                        if (n === "0") {
                            zeroCount++;
                        } else {
                            if (zeroCount > 0) {
                                chineseStr += cnNums[0];
                            }
                            // 归零
                            zeroCount = 0;
                            //alert(cnNums[parseInt(n)])
                            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
                        }
                        if (m === 0 && zeroCount < 4) {
                            chineseStr += cnIntUnits[q];
                        }
                    }
                    chineseStr += cnIntLast;
                }
                // 小数部分
                if (decimalNum !== "") {
                    const decLen = decimalNum.length;
                    for (let i = 0; i < decLen; i++) {
                        const n = decimalNum.substr(i, 1);
                        if (n !== "0") {
                            chineseStr += cnNums[Number(n)] + cnDecUnits[i];
                        }
                    }
                }
                if (chineseStr === "") {
                    chineseStr += cnNums[0] + cnIntLast + cnInteger;
                } else if (decimalNum === "") {
                    chineseStr += cnInteger;
                }
                return chineseStr;
            }
        }
    }
</script>

<style lang="scss" scoped>
    .center{
        margin: 15px;
    }
    .row{
        padding-bottom: 20px;
        font-size: 18px;
    }
    .table{
        margin-bottom: 10px;
        text-align: center;
    }

</style>
